<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="职位" prop="jobName">
        <el-input
          v-model="queryParams.jobName"
          placeholder="请输入职位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在园区" prop="address">
        <el-select clearable
                   v-model="queryParams.address"
                   placeholder="请选择园区"
        >
          <el-option
            v-for="item in parkList"
            :key="item.id"
            :label="item.parkName"
            :value="item.parkName"
          >
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="企业名称" prop="enterpriseName">-->
<!--        <el-input-->
<!--          v-model="queryParams.enterpriseName"-->
<!--          placeholder="请输入企业名称"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="企业" prop="enterpriseId">
<!--        <el-input-->
<!--          v-model="queryParams.enterpriseId"-->
<!--          placeholder="请输入企业id"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.enterpriseId" filterable
                   placeholder="请选择企业"
                   size="small"
        >
          <el-option
            v-for="item in qyEnterpriseList"
            :key="item.id"
            :label="item.enterpriseName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学历要求" prop="education">
        <el-input
          v-model="queryParams.education"
          placeholder="请输入学历要求"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['park:parkRecruitment:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['park:parkRecruitment:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['park:parkRecruitment:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['park:parkRecruitment:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parkRecruitmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="Id" align="center" prop="id" />-->
      <el-table-column label="职位" align="center" prop="jobName" />
      <el-table-column label="状态" align="center" prop="delFlag">
        <template slot-scope="scope">
          <span style="color: green" v-if="scope.row.delFlag==='0'">在招中</span>
          <span v-else style="color: red">已暂停</span>
        </template>
      </el-table-column>
      <el-table-column label="薪资(k)" align="center" prop="money" />
      <el-table-column label="位置" align="center" prop="address" />
      <el-table-column label="企业名称" align="center" prop="enterpriseName" />
<!--      <el-table-column label="企业id" align="center" prop="enterpriseId" />-->
      <el-table-column label="掌握技能" align="center" prop="skills" />
      <el-table-column label="福利" align="center" prop="welfares" />
      <el-table-column label="招聘人" align="center" prop="recruiter" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="人数规模" align="center" prop="scale" />
      <el-table-column label="工作年限" align="center" prop="seniority" />
      <el-table-column label="学历要求" align="center" prop="education" />
      <el-table-column label="职位描述" align="center" prop="describe" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['park:parkRecruitment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleStop(scope.row)"
            v-if="scope.row.delFlag==='0'"
            v-hasPermi="['park:parkRecruitment:remove']"
          >暂停</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['park:parkRecruitment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改园区企业招聘对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="职位" prop="jobName">
          <el-input v-model="form.jobName" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="薪资(k)" prop="jobName">
          <el-input v-model="form.money" placeholder="请输入薪资(格式为xxx-xxx)" />
        </el-form-item>
        <el-form-item label="所在园区" prop="address">
          <el-select clearable
                     v-model="form.address"
                     placeholder="请选择园区"
          >
            <el-option
              v-for="item in parkList"
              :key="item.id"
              :label="item.parkName"
              :value="item.parkName"
            >
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="企业名称" prop="enterpriseName">-->
<!--          <el-input v-model="form.enterpriseName" placeholder="请输入企业名称" />-->
<!--        </el-form-item>-->
        <el-form-item label="企业" prop="enterpriseId">
          <el-select v-model="form.enterpriseId" filterable
                     placeholder="请选择企业"
                     size="small"
                     @change="enterSelect"
          >
            <el-option
              v-for="item in qyEnterpriseList"
              :key="item.id"
              :label="item.enterpriseName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="掌握技能" prop="skills">
          <el-button size="small" @click="addSkill">添加技能</el-button>
          <div class="skill-col" v-for="(item,index) in skillList" :key="index">
            <el-input v-model="skillList[index]"></el-input>
            <el-button size="small" type="danger" @click="delSkill(index)">删除</el-button>
          </div>
        </el-form-item>
        <el-form-item label="福利" prop="welfares">
          <el-button size="small" @click="addWelfare">添加福利</el-button>
          <div class="skill-col" v-for="(item,index) in welfareList" :key="index">
            <el-input v-model="welfareList[index]"></el-input>
            <el-button size="small" type="danger" @click="delWelfare(index)">删除</el-button>
          </div>
        </el-form-item>
        <el-form-item label="招聘人" prop="recruiter">
          <el-input v-model="form.recruiter" placeholder="请输入招聘人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="人数规模" prop="scale">
          <el-input v-model="form.scale" placeholder="请输入人数规模(格式为xxx-xxx)" />
        </el-form-item>
        <el-form-item label="工作年限" prop="seniority">
          <el-input v-model="form.seniority" placeholder="请输入工作年限" />
        </el-form-item>
        <el-form-item label="学历要求" prop="education">
          <el-input v-model="form.education" placeholder="请输入学历要求" />
        </el-form-item>
        <el-form-item label="职位描述">
          <editor v-model="form.describe" :min-height="192"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listParkRecruitment, getParkRecruitment, delParkRecruitment, addParkRecruitment, updateParkRecruitment } from "@/api/park/parkRecruitment";
import Editor from '@/components/Editor';
import {listBuiPark} from "@/api/parkAssets/enterCommunity";
import {listQyEnterprise} from "@/api/smartParks/merchants/merchantsCompanySettlein";

export default {
  name: "ParkRecruitment",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      skillList:[],
      welfareList:[],
      parkList:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 园区企业招聘表格数据
      parkRecruitmentList: [],
      qyEnterpriseList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jobName: null,
        address: null,
        enterpriseName: null,
        enterpriseId: null,
        skills: null,
        welfares: null,
        recruiter: null,
        phone: null,
        scale: null,
        seniority: null,
        education: null,
        describe: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getParkList()
  },
  methods: {
    /** 查询园区企业招聘列表 */
    getList() {
      this.loading = true;
      listParkRecruitment(this.queryParams).then(response => {
        this.parkRecruitmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listQyEnterprise().then((response) => {
        this.qyEnterpriseList = response.rows;
      });
    },
    getParkList(){
      //查询园区列表，赋值给parkList;
      listBuiPark().then((response) =>{
        this.parkList = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        jobName: null,
        address: null,
        enterpriseName: null,
        enterpriseId: null,
        skills: null,
        welfares: null,
        recruiter: null,
        phone: null,
        scale: null,
        seniority: null,
        education: null,
        describe: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.skillList=[]
      this.welfareList=[]
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        name: "EditParRec"
      });
    },
    handleStop(e){
      this.form=e
      this.$confirm('是否确认暂停招聘', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.form.delFlag='1'
        updateParkRecruitment(this.form)
      }).then(() => {
        this.getList();
        this.msgSuccess("暂停成功");
      })


    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.$router.push({
        name: "EditParRec",
        query: { id: row.id || this.ids },
      });
      // const id = row.id || this.ids
      // getParkRecruitment(id).then(response => {
      //   this.form = response.data;
      //   this.skillList=this.form.skills.split(',')
      //   this.welfareList=this.form.welfares.split(',')
      //   this.open = true;
      //   this.title = "修改园区企业招聘";
      // });
    },
    addSkill(){
      if (this.skillList.length>4){
        this.msgError('添加不能超过5个！')
        return
      }
      this.skillList.push('')
    },
    delSkill(index){
      this.skillList.splice(index,1)
    },
    addWelfare(){
      if (this.welfareList.length>4){
        this.msgError('添加不能超过5个！')
        return
      }
      this.welfareList.push('')
    },
    delWelfare(index){
      this.welfareList.splice(index,1)
    },
    enterSelect(e){
      let na=this.qyEnterpriseList.find(item=>{
        if (item.id===e)return item
      })
      this.form.enterpriseName=na.enterpriseName
    },
    /** 提交按钮 */
    submitForm() {
      this.form.skills=this.skillList.toString()
      this.form.welfares=this.welfareList.toString()
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateParkRecruitment(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParkRecruitment(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除园区企业招聘编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delParkRecruitment(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('park/parkRecruitment/export', {
        ...this.queryParams
      }, `park_parkRecruitment.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
.skill-col{
  margin: 4px 0;
  display: flex;
  button{
    margin-left: 6px;
  }
}
</style>
